using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using WowAceUpdater.Properties;

namespace WowAceUpdater
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
        }

        private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Settings.Default.Save();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dr = folderBrowserDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
                textBox1.Focus();
            }

        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            string wow = WowEnvironment.GetInstallFolder();

            if (string.IsNullOrEmpty(textBox1.Text))
            {
                if (!string.IsNullOrEmpty(wow))
                {
                    if (Directory.Exists(wow))
                    {
                        textBox1.Text = wow;
                        errorProvider1.SetError(textBox1, "");
                    }
                }
            }

            if (textBox1.Text != FileSystem.CheckPathString(textBox1.Text))
            {
                errorProvider1.SetError(textBox1, "Invalid Path String");
                e.Cancel = true;
            }

            if (!new DirectoryInfo(textBox1.Text).Exists)
            {
                errorProvider1.SetError(textBox1, "Directory Does Not Exist");
                e.Cancel = true;
            }

            if (!e.Cancel)
                errorProvider1.SetError(textBox1, "");

            
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                textBox1.Text =
                        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                errorProvider1.SetError(textBox1, "");
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            bool bErr = false;

            if (textBox1.Text != FileSystem.CheckPathString(textBox1.Text))
            {
                errorProvider1.SetError(textBox1, "Invalid Path String");
                bErr = true;
            }

            if (!Directory.Exists(textBox1.Text))
            {
                bErr = true;
                errorProvider1.SetError(textBox1, "Directory Does Not Exist");
            }

            if (!bErr)
                errorProvider1.SetError(textBox1, "");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            bool bErr = false;

            if (textBox1.Text != FileSystem.CheckPathString(textBox1.Text))
            {
                errorProvider1.SetError(textBox1, "Invalid Path String");
                bErr = true;
            }

            if (!Directory.Exists(textBox1.Text))
            {
                bErr = true;
                errorProvider1.SetError(textBox1, "Directory Does Not Exist");
            }

            if (!bErr)
                errorProvider1.SetError(textBox1, "");
        }
    }
}